#include <Wire.h>
float distance = 0;  //距离数据十进制值
float ds[3];  //创建一个浮点数的数组，用于储存读取到的值
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define btnPin1 2  //定义按钮引脚
//变量,用于接收按钮检测到的值
boolean btnVal1;
int count;
boolean ranging_state = 0;  //用于是否开始测距

void setup() {
  Serial.begin(9600);
  Wire.begin();
  pinMode(btnPin1,INPUT);  //引脚11设置为输入状态

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

   // Clear the buffer
  display.clearDisplay();
  display.display();
}

void loop() {
  btnVal1 = digitalRead(btnPin1);  //读取按钮值，并赋给btnVal1
  if(btnVal1 == 0)  //如果按钮1被按下了
  {
    boolean i = 1;
    while(i == 1)
    {
      btnVal1 = digitalRead(btnPin1);  //读取按钮值，并赋给btnVal1
      if(btnVal1 == 1)
      {
        count++;
        Serial.println(count);
        i = 0;
      }
    }
    ranging_state = count % 2; //将点击按钮的次数对2求余数, 单数等于1,双数等于0
  }
  if(ranging_state == 1)
  {
    distance = checkdistance();  //读取超声波传感器测到的值
    display.setTextSize(2);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(0,0);             // Start at top-left corner
    display.println(F("Distance : "));
    display.println(distance);
    display.display();
    display.clearDisplay();
  }
  else
  { 
    display.setTextSize(2);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(0,0);             // Start at top-left corner
    display.println(F("Distance : "));
    display.println(distance);
    display.display();
    display.clearDisplay();
  }
}

//超声波测距函数
float checkdistance() {
  char i = 0;
  ds[0]=0;
  ds[1]=0;
  ds[2]=0;
  Wire.beginTransmission(0x57);   //地址为0X57 写8位数据为AE，读8位数据位AF
  Wire.write(1);                  //写命令0X01,0X01为开始测量命令 
  Wire.endTransmission();            
  delay(120);                     //测量周期延时，一个周期为100mS,设置120MS,留余量    
  Wire.requestFrom(0x57,3);       //地址为0X57 读3个8位距离数据       
  while (Wire.available())
  {
   ds[i++] = Wire.read();
  }          
  distance=(ds[0]*65536+ds[1]*256+ds[2])/10000; //计算成CM值     
  Serial.print("distance : "); 
  if ((1<=distance)&&(600>=distance))           //1CM-6M之间数值显示 
  {
  Serial.print(distance);
   Serial.print(" CM ");  
  }
  else 
  {
  Serial.print(" - - - - ");                   //无效数值数值显示 - - - - 
  }
  Serial.println();    
  delay(10);                                  //调节测量周期  
  return distance;
}
